﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Runda.CMS.Bll;
using System.Data;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.Budget
{
    public partial class SalesBudgetResult : BasePage
    {
        public string SortString
        {
            get
            {
                if (ViewState["SortString"] == null)
                {
                    ViewState["SortString"] = "ProductManager";
                }

                return ViewState["SortString"].ToString();
            }
            set { ViewState["SortString"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2012; i <= 2015; i++)
                {
                    ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                BindGrid();
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "reject")
            {
                BudgetBll bll = new BudgetBll();

                bll.RejectBudget(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProcessID"].ToString());
            }

            if (e.CommandName.ToLower() == "confirm")
            {
                BudgetBll bll = new BudgetBll();

                bll.CheckBudget(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProcessID"].ToString(), base.CurrentUserEntity.UserName.ToString());
            }

            if (e.CommandName.ToLower() == "return")
            {
                BudgetBll bll = new BudgetBll();

                bll.ReturnBudget(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProcessID"].ToString(), base.CurrentUserEntity.UserName.ToString());
            }

            BindGrid();

        }

        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                Label lblSubmmitStatus = e.Item.FindControl("lblSubmmitStatus") as Label;
                Label lblSubmmitStatusName = e.Item.FindControl("lblSubmmitStatusName") as Label;
                Label lblCheckStatus = e.Item.FindControl("lblCheckStatus") as Label;
                Label lblCheckStatusName = e.Item.FindControl("lblCheckStatusName") as Label;
                LinkButton lnkbtnReject = e.Item.FindControl("lnkbtnReject") as LinkButton;
                LinkButton lnkbtnConfirm = e.Item.FindControl("lnkbtnConfirm") as LinkButton;
                LinkButton lnkbtnReturn = e.Item.FindControl("lnkbtnReturn") as LinkButton;

                lblSubmmitStatusName.Text = (lblSubmmitStatus.Text == "0" ? "编制中" : "已提交");
                lblCheckStatusName.Text = (lblCheckStatus.Text == "0" ? "" : "已确认");
                if (lnkbtnReject != null && lnkbtnConfirm != null)
                {
                    DataRowView dv = e.Item.DataItem as DataRowView;
                    int iSubmitStatus, iCheckStatus;

                    lnkbtnReject.Visible = false;
                    lnkbtnConfirm.Visible = false;
                    lnkbtnReturn.Visible = false;
                    iSubmitStatus = new BudgetBll().GetBudgetSubmitStatus(ddlBudgetYear.SelectedValue.ToString(), dv["ProductManager"].ToString());
                    iCheckStatus = new BudgetBll().GetBudgetCheckStatus(ddlBudgetYear.SelectedValue.ToString(), dv["ProductManager"].ToString());
                    if (iSubmitStatus > 0 && base.CheckUserHasFuction(1, "Func10012") && iCheckStatus == 0)
                    { lnkbtnConfirm.Visible = true; lnkbtnReject.Visible = true; }

                    if (iSubmitStatus > 0 && base.CheckUserHasFuction(1, "Func10013") && iCheckStatus > 0)
                    { lnkbtnReturn.Visible = true; }
                    lnkbtnReturn.OnClientClick = "return OnDelConfirm();";
                }
            }
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindGrid();
        } 

        private void BindGrid()
        {
            string sb = getWhereClause();

            DataTable dt = new BudgetBll().QuerySalesBudgetResult(sb, SortString);

            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();
        }

        
        protected void gridAccessoryList_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e)
        {
            string OrderBy = e.SortExpression + " " + (e.NewSortOrder == Telerik.WebControls.GridSortOrder.Ascending ? "asc" : "desc");
            SortString = OrderBy;
            BindGrid();
            

        }
        private string getWhereClause()
        { 
            StringBuilder sb = new StringBuilder();

            if (ddlBudgetYear.SelectedValue != string.Empty)
            {
                sb.Append(" and [BudgetYear]  = '").Append(ddlBudgetYear.SelectedValue).Append("'");
            }

            

            return sb.ToString();
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string Whereclause = getWhereClause();

            BudgetBll bll = new BudgetBll();

            DataTable dt = bll.ExportSalesBudgets(Whereclause, " [客户] asc ");

            Export.Export2Excel(dt, "销售预算一览表", Page.Response);

        }

        
    }
}
